/*
 * @Author: biankai
 * @Date: 2021-11-04 14:27:50
 * @LastEditTime: 2021-11-05 11:11:29
 * @LastEditors: biankai
 * @FilePath: /gitee/devops_service/src/services/kubernetes/namespace.service.ts
 */
import {
    Injectable,
    Inject,
    HttpService,
} from '@nestjs/common';
import * as kubernetes from '@kubernetes/client-node';
import { BaseService } from './base.service';
@Injectable()
export class NamespaceService extends BaseService {
    private readonly k8sApi: kubernetes.CoreV1Api;

    constructor(@Inject(HttpService) private readonly httpService: HttpService) {
        super()
        const kc = new kubernetes.KubeConfig();
        kc.loadFromFile(this.k8sConfig)
        this.k8sApi = kc.makeApiClient(kubernetes.CoreV1Api);
    }
    async listK8sNamespace() {
        const resBody = await this.k8sApi.listNamespace();
        return await resBody.body;
    }
    async singleK8sNamespace(name) {
        const resBody = await this.k8sApi.readNamespace(name);
        return await resBody.body;
    }
    async createK8sNamespace(namespace) {
        var options = {
            metadata: {
                name: namespace.name
            }
        };
        const resBody = await this.k8sApi.createNamespace(options);
        return await resBody.body;
    }
    async replaceK8sNamespace(name, nowNamespace) {
        var options = {
            metadata: {
                name: nowNamespace.name
            }
        };
        const resBody = await this.k8sApi.replaceNamespace(name, options);
        return await resBody.body;
    }
    async deleteK8sNamespace(name) {
        const resBody = await this.k8sApi.deleteNamespace(name);
        return await resBody.body;
    }
}
